#!/bin/bash
echo -e "------------------------------------------------------------------------------------------"
echo "此脚本用来组装语音技术部sdk资源，关注##############输出的文件  @auther 李鹏程"
echo "此脚本用来组装语音技术部sdk资源，关注##############输出的文件  @auther 李鹏程"
echo "此脚本用来组装语音技术部sdk资源，关注##############输出的文件  @auther 李鹏程"
echo -e "------------------------------------------------------------------------------------------"

# 筛选文件
traverse() {
    for file in "$1"/*; do
        if [ -d "$file" ]; then
            traverse "$file"
        elif [ -f "$file" ]; then
          filename=$(basename $file)
          if [[ "${arrayResItem[@]}" =~ "$filename" ]];then
            cp $file $resultResDir
          elif [[ "${arraySoItem[@]}" =~ "$filename" ]];then
            if [[ "$(dirname $file)" =~ "arm64" ]];then
              cp -f $file $resultArm64Dir
            elif [[ "$(dirname $file)" =~ "arm" ]];then
              cp -f $file $resultArmDir
            else
              echo "others::$file###################"
            fi
          elif echo "$file" | grep "bdasr_driver.*.aar" | grep -q "/sdk.*release/";then
            cp -f "$file" $resultDir
          elif echo "$file" | grep -q "model.*.dat";then
            cp -f "$file" $resultDir
          elif [[ "$filename" == "MD5.md" ]];then
            cp -f "$file" $resultDir
          elif [[ "$filename" == "android.tar" ]];then
            rm -rf "$(dirname $file)/android/"
            mkdir -p "$(dirname $file)/android/"
            tar -xf "$file" -C "$(dirname $file)/android/"
            traverse "$(dirname $file)/android/"
          else
            echo "无用文件$file###################"
          fi
        else
          echo "not file, not dir, $file###################"
        fi
    done
}

# 比对md5值
compareMD5() {
  for file in $(ls $1/*);do
    if [[ -f "$file" ]]; then
      filename=$(basename $file)
      if [[ "${arrayMd5Item[@]}" =~ "$2$filename" ]]; then
        md5Value=$(md5 $file | sed s/" "/""/g | awk -v FS="=" '{printf "%s", $2}')
        printf "$file=${md5Value}"
        if [[ "${arrayMd5Item[@]}" =~ "${md5Value}" ]]; then
          echo " md5值一致"
        else
          echo " md5值不一致###################"
        fi
      else
        echo "MD5.md不包含$file###################"
      fi
    fi
  done
}

sdkDirPath=$1
if [[ -z $sdkDirPath ]];then
  read -p "填入语音技术部sdk资源路径:" sdkDirPath;
  if [[ -z $sdkDirPath ]];then
    exit
  fi
fi

if [ -d $sdkDirPath ];then
  echo "${sdkDirPath} is dir"
else
  echo "${sdkDirPath} is not dir; And exit"
  exit
fi

resultDir=$(dirname $sdkDirPath)/pack/
resultResDir=$resultDir/res/
resultArmDir=$resultDir/res/arm
mkdir -p $resultArmDir
resultArm64Dir=$resultDir/res/arm64
mkdir -p $resultArm64Dir

echo "----------------------筛选文件start-------------------------"

# 需要筛选的资源文件
arrayResItem=("audio_config.json" "dvad.mdl" "esis_usr_aks.pkg" "esis_xiaodu_wak.pkg"
"model_aec.mdl" "nnns.mdl.mdl")
# arm和arm64下需要筛选的so文件
arraySoItem=("libbdSPILAudioProc.so" "libbdTinyEasrAndroid.so" "libdoavad.so"
"libesis-aks.so" "libesis-wkp.so" "libhouyi_aec.so")

traverse $sdkDirPath

echo "----------------------筛选文件end-------------------------"
echo "----------------------比对MD5值start----------------------"

arrayMd5Item=()
for md5Value in $(cat $resultDir/MD5.md | tr " " "_");do
#  echo "md5Value=$md5Value"
  arrayMd5Item+=("$md5Value")
done
#echo "arrayMd5Item = ${arrayMd5Item[@]}"

compareMD5 $resultResDir ""
compareMD5 $resultArmDir "/arm/"
compareMD5 $resultArm64Dir "/arm64/"

echo "----------------------比对MD5值end----------------------"

